package com.sympsel.Optimal_Algorithm_OA;

import org.junit.Test;

import java.util.*;

public class HashAlgorithm {
    @Test
    public void test1() {
        Set<Integer> set = new HashSet<>();
        set.add(1);
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        Set<Integer> integers = map.keySet();
        for (Integer integer : integers) {
            System.out.println(integer);
        }
        Collection<Integer> values = map.values();
        for (Integer value : values) {
            System.out.println(value);
        }
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            String sortedStr = sort(str);
            if (!map.containsKey(sortedStr)) {
                map.put(sortedStr, new ArrayList<>() {{
                    add(str);
                }});
            } else {
                map.get(sortedStr).add(str);
            }
        }
        return new ArrayList<>(map.values());
    }

    private String sort(String str) {
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        return new String(chars);
    }

    @Test
    public void test2() {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> lists = groupAnagrams(strs);
        for (List<String> list : lists) {
            for (String s : list) {
                System.out.print(s + " ");
            }
            System.out.println();
        }
    }
}
